package com.whfc.ms.api.sys.controller;

import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.entity.FileUploadInfo;
import com.whfc.entity.dto.Apply.rep.EntryApplicationListRep;
import com.whfc.entity.dto.Apply.rep.FileUploadInfoRep;
import com.whfc.entity.dto.Apply.req.AppEntryApplicationReq;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.ms.api.sys.service.SysFileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xjy
 * @description 文件上传
 * @date 2019-09-20
 */
@RestController
@RequestMapping("/mp/api/file")
public class SysFileUploadController {

    @Autowired
    private SysFileUploadService fileUploadInfoService;

    /**
     * 查询全部文件列表
     */
    @GetMapping("/list")
    public Result getAllFileList(AppEntryApplicationReq req) {
        List<FileUploadInfoRep> pageData = fileUploadInfoService.getAllWxFileList(req);
        return ResultUtil.success(pageData);
    }

    /**
     * 保存文件信息
     */
    @PostMapping("/upload")
    public Result uploadFile(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody FileUploadInfo fileInfo) {
        // 1. 校验文件是否为空
        int rows = fileUploadInfoService.uploadWxFile(user,fileInfo);
        if (rows > 0) {
            return ResultUtil.success("文件上传成功，文件ID：" + fileInfo.getId());
        } else {
            return ResultUtil.failure("文件信息保存失败");
        }
    }

    /**
     * 根据文件ID查询文件地址
     */
    @GetMapping("/url/{id}")
    public Result getFileUrlById(@PathVariable Integer id) {
        if (id == null) {
            return ResultUtil.failure("文件ID不能为空");
        }
        String fileUrl = fileUploadInfoService.getWxFileUrlById(id);
        if (fileUrl != null) {
            return ResultUtil.success(fileUrl, "查询文件地址成功");
        } else {
            return ResultUtil.failure("未找到对应文件");
        }
    }
}
